/**
 * Desc：策略模式(Strategy),定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户。.
 * Created by VincentZee on 2019/3/2
 */
public class StrategyPatternDemo {
    private static double total = 0.0D;
    public static void main(String[] args) {
        // 策略与简单工厂结合实现
        CashContext cashContext1 = new CashContext("满300返100");
        CashContext cashContext2 = new CashContext("正常收费");
        total += cashContext1.GetResult(700*3);
        total += cashContext2.GetResult(400);

        // 策略模式实现
        /*total += AssClient.calMoney("700","3","满300返100");
        total += AssClient.calMoney("400","1","正常收费");*/
        System.out.println(total);
    }
}

// 策略模式实现
/*class AssClient {
    public static double calMoney(String inputPrice, String inputNum, String type) {
        CashContext cashContext = null;

        switch (type) {
            case "正常收费":
                cashContext = new CashContext(new CashNormal());
                break;
            case "满300返100":
                cashContext = new CashContext(new CashReturn("300", "100"));
                break;
            case "打8折":
                cashContext = new CashContext(new CashRebate("0.8"));
                break;
            default:
                break;
        }

        double totalPrices = 0D;
        totalPrices = cashContext.GetResult(Double.valueOf(inputPrice) * Double.valueOf(inputNum));
        return totalPrices;
    }
}*/

class CashContext {
    private CashSuper cashSuper;

    // 策略模式实现
    /*public CashContext(CashSuper cashSuper) {
        this.cashSuper = cashSuper;
    }*/

    // 策略与简单工厂结合
    public CashContext(String type){
        switch (type) {
            case "正常收费":
                cashSuper = new CashNormal();
                break;
            case "满300返100":
                cashSuper = new CashReturn("300", "100");
                break;
            case "打8折":
                cashSuper = new CashRebate("0.8");
                break;
            default:
                break;
        }
    }

    public double GetResult(double money) {
        return cashSuper.acceptCash(money);
    }
}

abstract class CashSuper {
    public abstract double acceptCash(double money);
}

/**
 * 正常收费子类
 */
class CashNormal extends CashSuper {

    @Override
    public double acceptCash(double money) {
        return money;
    }
}

/**
 * 打折收费子类
 */
class CashRebate extends CashSuper {
    private double moneyRabate = 1d;

    public CashRebate(String moneyRabate) {
        this.moneyRabate = Double.valueOf(moneyRabate);
    }

    @Override
    public double acceptCash(double money) {
        return money * moneyRabate;
    }
}

/**
 * 返利收费子类
 */
class CashReturn extends CashSuper {
    private double moneyCondition = 0.0D;
    private double moneyReturn = 0.0D;

    public CashReturn(String moneyCondition, String moneyReturn) {
        this.moneyCondition = Double.valueOf(moneyCondition);
        this.moneyReturn = Double.valueOf(moneyReturn);
    }

    @Override
    public double acceptCash(double money) {
        double result = money;
        if (money >= moneyCondition) {
            result = money - Math.floor(money / moneyCondition) * moneyReturn;
        }
        return result;
    }
}